package com.example.bys.zyfypt_103Fbw.model.collect;

import com.example.bys.zyfypt_103Fbw.bean.collect.AllBean;
import com.example.bys.zyfypt_103Fbw.bean.collect.CollectBean;
import com.example.bys.zyfypt_103Fbw.iface.collect.CollectListListener;
import com.example.bys.zyfypt_103Fbw.iface.collect.CollectListiface;
import com.example.bys.zyfypt_103Fbw.service.collect.CollectListService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by BYS on 2017/5/24.
 */

public class CollectListModel implements CollectListiface<AllBean> {
    private Retrofit retrofit;
    private String BASEURL
            ="http://amicool.neusoft.edu.cn/";
    //构造函数
    public CollectListModel()
    {   //使用Retrofit----1
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getResultList(String mod, int page, String sessionID, final CollectListListener<AllBean> listener) {
        CollectListService collectListService =retrofit.create(CollectListService.class);
        Call<List<CollectBean<AllBean>>> call =collectListService.getAllList(mod,page,sessionID);
        call.enqueue(new Callback<List<CollectBean<AllBean>>>() {
            @Override
            public void onResponse(Call<List<CollectBean<AllBean>>> call, Response<List<CollectBean<AllBean>>> response) {
                if(response.isSuccessful() && response!=null)
                {
                    listener.onResponse(response.body());
                }
                else
                    listener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<CollectBean<AllBean>>> call, Throwable t) {
                listener.onFail(t.toString());
            }
        });
    }
}
